import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

/**
 * Class that handles the events from the Destination selection screen
 * @author Paula Gonz�lez Mu�oz
 * @author Alex Bruck
 * @author Cem Altel
 */
public class DestinationListener implements CommandListener
{
	/**
 	 * GUI element which events the DestinationListener is going to handle
 	 */
	GUI m;
 	
	/**
 	 * Constructor
 	 * @param m GUI element which events the DestinationListener is going to handle
 	 */
 	public DestinationListener(GUI m)
 	{
 		this.m = m;
 	}
	
 	/** 
 	 * Method that handles the events from the destination selection screen
 	 * @param c event to handle
 	 * @param d displayable element to interact with
 	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
 	 */
 	public void commandAction(Command c, Displayable d)
 	{	
 		String label = c.getLabel();
		
		if(label.equals("Exit"))
		{
			try
			{
				m.destroyApp(true);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (label.equals("Back"))
		{

			m.newTravel();
		}
		else
 		{
 			List down = (List)m.display.getCurrent();
 			//if we have the vector with alphabetically ordered stops
 			m.destination = (String)m.list_stops.elementAt(down.getSelectedIndex());
 			//in the end we go back to the "from/to screen"
 			m.newTravel();
 		}
 	}
}